//
//  ViewController.m
//  AutoresizingMask
//
//  Created by 韩艳锋 on 2021/12/21.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIView * viewBg;
@property (nonatomic, strong) UIView * viewMask;

@property (nonatomic, strong) NSTimer * timer;

@property (nonatomic, assign) NSUInteger scale;

@property (nonatomic, assign) BOOL add;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat width = self.view.frame.size.width;
    CGFloat heigh = self.view.frame.size.height;
    self.viewBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0,width/2, heigh/2)];
    self.viewBg.backgroundColor = [UIColor redColor];
    self.viewBg.autoresizingMask = YES;
    [self.view addSubview:self.viewBg];
    self.viewMask = [[UIView alloc] initWithFrame:CGRectMake(width/8, heigh/8, width/4, heigh/4)];
    self.viewMask.backgroundColor = [UIColor greenColor];
    [self.viewBg addSubview:self.viewMask];
    self.add = YES;
    [self layout];
    [self timerInit];
}


- (void)timerInit {
        
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1/60.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    [self.timer fire];
}

- (void)timerAction {
    
    NSUInteger sc = 4;
    NSUInteger val = (60 * sc);
    if (self.scale == 0) {
        self.add = YES;
    }
    
    if (self.scale == val) {
        self.add = NO;
    }
    
    if (self.add) {
        self.scale = self.scale + 1;
    } else {
        self.scale = self.scale - 1;
    }
    
    CGFloat sca = self.scale * 0.5 / val  + 0.5;
    self.viewBg.frame = CGRectMake(0, 0, self.view.frame.size.width * sca, self.view.frame.size.height * sca);
    
}

- (void)layout {
    // 我的理解
    //  UIViewAutoresizingNone 相当于 UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin
    //  分为两组水平方向A组LeftMargin、RightMargin、Width和竖直方向B组TopMargin、BottomMargin、Height
    //  如果指定6个中的一个，例如UIViewAutoresizingFlexibleLeftMargin，则左侧的距离随着父视图变化，剩下的水平方向（宽度、和右侧的距离）不变
    //  如果指定同一组中的两个及三个，则其随父视图变化的比例相同
    
    self.viewMask.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin |  UIViewAutoresizingFlexibleRightMargin
//        UIViewAutoresizingFlexibleLeftMargin
//     UIViewAutoresizingFlexibleRightMargin
//    | UIViewAutoresizingFlexibleTopMargin
//    | UIViewAutoresizingFlexibleBottomMargin
//    | UIViewAutoresizingFlexibleHeight
//    | UIViewAutoresizingFlexibleWidth
    ;
    
//    UIViewAutoresizingNone                 = 0,
//    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
//    UIViewAutoresizingFlexibleWidth        = 1 << 1,
//    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
//    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
//    UIViewAutoresizingFlexibleHeight       = 1 << 4,
//    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
    

}

@end
